package com.wbs.shop.nuby.utils;

import android.app.Activity;
import android.content.Context;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.wbs.shop.nuby.R;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumFile;
import com.yanzhenjie.album.api.widget.Widget;

import java.util.ArrayList;

/**
 * Created by zhoucl on 2018/4/13 0013.
 */
public class ImageUploadUtil {
    private static ArrayList<String> MIME_TYPE = new ArrayList<>();
    private static long MIN_SIZE = 50 * 1024;
    private Activity activity;
    private Fragment fragment;
    private Widget mWidget;
    private OnUploadSuccessListener mOnUploadSuccessListener;
    private OnUploadCancelListener mOnUploadCancelListener;

    static {
        MIME_TYPE.add("image/jpg");
        MIME_TYPE.add("image/jpeg");
        MIME_TYPE.add("image/png");
        MIME_TYPE.add("image/JPG");
        MIME_TYPE.add("image/JPEG");
        MIME_TYPE.add("image/PNG");
    }

    public ImageUploadUtil(Activity activity) {
        this.activity = activity;
        init();
    }

    public ImageUploadUtil(Fragment fragment) {
        this.fragment = fragment;
        init();
    }

    private void init() {
        Context content = getContext();
        if (content == null)
            return;
        mWidget = Widget.newDarkBuilder(content)
                .statusBarColor(ContextCompat.getColor(content, R.color.main_color))
                .toolBarColor(ContextCompat.getColor(content, R.color.main_color))
                .build();
    }


    public void selectImage() {
        Context content = getContext();
        if (content == null)
            return;
        Album.image(content)
                .singleChoice()
                .afterFilterVisibility(false)
                .camera(true)
                .filterMimeType(attributes -> !MIME_TYPE.contains(attributes))
                .filterSize(attributes -> attributes < MIN_SIZE)
                .widget(mWidget)
                .onResult(this::onAction)
                .onCancel(this::onCancel)
                .start();
    }

    public void gallery(ArrayList<String> gallery, int currentPosition) {
        Context content = getContext();
        if (content == null)
            return;
        Album.gallery(content)
                .currentPosition(currentPosition)
                .checkedList(gallery)
                .checkable(false)
                .widget(mWidget)
                .onCancel(this::onCancel)
                .start();
    }

    private void onCancel(String result) {
        if (mOnUploadCancelListener != null)
            mOnUploadCancelListener.onCancel();
    }

    private void onAction(ArrayList<AlbumFile> result) {
        if (!result.isEmpty()) {
            if (mOnUploadSuccessListener != null) {
                mOnUploadSuccessListener.onUploadSuccess(result);
            }
        }
    }

    public void setOnUploadSuccessListener(OnUploadSuccessListener mOnUploadSuccessListener) {
        this.mOnUploadSuccessListener = mOnUploadSuccessListener;
    }

    public void setOnUploadCancelListener(OnUploadCancelListener mOnUploadCancelListener) {
        this.mOnUploadCancelListener = mOnUploadCancelListener;
    }

    public interface OnUploadSuccessListener {
        void onUploadSuccess(ArrayList<AlbumFile> imageUploads);
    }

    public interface OnUploadCancelListener {
        void onCancel();
    }

    public void onDestroy() {
        activity = null;
        fragment = null;
        mOnUploadSuccessListener = null;
        mWidget = null;
    }

    private Context getContext() {
        if (activity != null) {
            return activity;
        } else if (fragment != null) {
            return fragment.getContext();
        }
        return null;
    }

    private Activity getActivity() {
        if (activity != null) {
            return activity;
        } else if (fragment != null) {
            return fragment.getActivity();
        }
        return null;
    }
}
